package com.lantosec.hmrp.modules.eval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.modules.eval.entity.EvalItem;
import com.lantosec.hmrp.modules.eval.mapper.EvalItemMapper;
import com.lantosec.hmrp.modules.eval.service.IEvalItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.eval.service.IEvalTypeService;
import com.lantosec.hmrp.modules.eval.vo.EvalItemVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 考评项目定义 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-13
 */
@Service
@Primary
@Log4j2
public class EvalItemServiceImpl extends ServiceImpl<EvalItemMapper, EvalItem> implements IEvalItemService {

    @Autowired
    private IEvalTypeService iEvalTypeService;

    @Override
    public PageVO<EvalItemVO> getAll(PageVO<EvalItem> page, String name, Integer evalTarget, Integer rewardOrientation, String evalTypeId) {
        PageVO<EvalItem> evalItemPage = new PageVO<>();
        PageVO<EvalItemVO> evalItemPageVO = new PageVO<>();
        if (StringUtils.isNotEmpty(name)) {
            evalItemPage = this.page(page, new QueryWrapper<EvalItem>()
                    .eq(evalTarget != null, EvalItem.ColName.eval_target.toString(), evalTarget)
                    .eq(rewardOrientation != null, EvalItem.ColName.reward_orientation.toString(), rewardOrientation)
                    .eq(evalTypeId != null, EvalItem.ColName.eval_type_id.toString(), evalTypeId)
                    .and(i -> i.or().like(EvalItem.ColName.eval_item_name.toString(), name)
                            .or().like(EvalItem.ColName.eval_item_content.toString(), name)
                            .or().like(EvalItem.ColName.standard.toString(), name)
                            .or().like(EvalItem.ColName.remark.toString(), name))
                    .orderByAsc(EvalItem.ColName.eval_item_order.toString()));
        } else {
            evalItemPage = this.page(page, new QueryWrapper<EvalItem>().eq(evalTarget != null, EvalItem.ColName.eval_target.toString(), evalTarget)
                    .eq(rewardOrientation != null, EvalItem.ColName.reward_orientation.toString(), rewardOrientation)
                    .eq(evalTypeId != null, EvalItem.ColName.eval_type_id.toString(), evalTypeId)
                    .orderByAsc(EvalItem.ColName.eval_item_order.toString()));
        }
        List<EvalItem> evalItem = evalItemPage.getRecords();
        //查询考评项目分类ID对应的名称
        List<EvalItemVO> evalItemVOS = new ArrayList<>();
        evalItem.forEach(parm -> {
            EvalItemVO evalItemVO = parm.convertToVO();
            if (StringUtils.isNotEmpty(parm.getEvalTypeId())) {
                String evalTypeName = iEvalTypeService.getById(parm.getEvalTypeId()).getEvalTypeName();
                evalItemVO.setEvalTypeName(evalTypeName);
            }
            evalItemVOS.add(evalItemVO);

        });
        evalItemPageVO.setRecords(evalItemVOS);
        evalItemPageVO.setPageNo(evalItemPage.getPageNo());
        evalItemPageVO.setPageSize(evalItemPage.getPageSize());
        evalItemPageVO.setTotal(evalItemPage.getTotal());
        return evalItemPageVO;
    }

    @Override
    public void updateOrder(String evalItemId, int evalItemOrder) {
        EvalItem evalItem = new EvalItem();
        evalItem.setEvalItemOrder(evalItemOrder);
        this.update(evalItem, new QueryWrapper<EvalItem>().eq(EvalItem.ColName.eval_item_id.toString(), evalItemId));
    }
}
